<?php
//require section
require_once dirname(__FILE__) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_session.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_tickets.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_redirect.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_pager.php';
$title = 'Book Preview';

$ticket_quantity = filter_input(INPUT_POST, "txt_tickets_quantity");
$city_id = filter_input(INPUT_POST, "ddl_city");
//$cinema_id = filter_input(INPUT_POST, "ddl_cinema");
$movie_id = filter_input(INPUT_POST, "ddl_movie");
$show_date = filter_input(INPUT_POST, "ddl_show_date");
//$time_start = filter_input(INPUT_POST, "ddl_time_start");


if (!empty($ticket_quantity) && !empty($city_id) 
        && !empty($movie_id) && !empty($show_date)) {
    $_SESSION['book_info']['status'] = "started";
    $_SESSION['book_info']['ticket_quantity'] = $ticket_quantity;
    $_SESSION['book_info']['city_id'] = $city_id;
    $_SESSION['book_info']['movie_id'] = $movie_id;
    $_SESSION['book_info']['show_date'] = $show_date;
    $user_id = $_SESSION['user_info']['id'];
    $_SESSION['book_info']['user_id'] = $user_id;
}

require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_inside_booking.php';


$ticket = api_tickets::get_ticket_by_ticket_booking(
        $_SESSION['book_info']['city_id'], 0, 
        $_SESSION['book_info']['movie_id'], 
        $_SESSION['book_info']['show_date'], "");
$ticket['ticket_quantity'] = $_SESSION['book_info']['ticket_quantity'];

?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php';
        
        ?>
        <script type="text/javascript">
            function accept(id) {
                $.ajax({
                    type: "POST",
                    url: "<?php echo LINK_ROOT . DIR_AJAX; ?>accept_cinema_and_time_start.php",
                    data: "ticket_id="+id,
                    success: function (result) {
                        $("#your_choice").html(result);
                        $('#accepted').modal('show');
                        $('#accepted_content').html('Your choice now is: '+result);
                    }
                });
            }
        </script>
    </head>
    <body>
        <div class="ts-body-container">
            <div class="ts-wrapper">
                <div class="padding-top-large row">
                    <?php
                    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php';
                    
                    
                    ?>
                </div>
                <div class="ui grid">
                    <div class="row">
                        <div class="column">
                            <!-- Step -->
                            <div id='booking_step' class="ui four steps margin-top-normal">
                                <div class="ui active step">
                                    Choose Cinema & Time Start
                                </div>
                                <div class="ui disabled step">
                                    Choose Seats
                                </div>
                                <div class="ui disabled step">
                                    Choose Combo
                                </div>
                                <div class="ui disabled step">
                                    Coupon Code & Finish
                                </div>
                            </div>
                            <!-- End Step -->

                            <h1>Choose Cinema & Time Start</h1>

                            <?php
                            
                            ?>
                            <form id="form_tickets_quantity" action="" method="post" name="form_tickets_quantity">
                                <table class="ui orange table segment">
                                    <tr>
                                        <th style="width: 30em;">City:</th>
                                        <td><?php echo $ticket['city_name']; ?></td>
                                    </tr>
                                    <tr>
                                        <th>Movie:</th>
                                        <td><?php echo $ticket['movie_title']; ?></td>
                                    </tr>
                                    <tr>
                                        <th>Date:</th>
                                        <?php
                                            $date = date_create($ticket["show_date"]);
                                            $date_str = date_format($date, "d/m/Y");
                                        ?>
                                        <td><?php echo $date_str; ?></td>
                                    </tr>
                                    <tr>
                                        <th>Tickets Quantity:</th>
                                        <td class="ui left icon input">
                                            <?php echo $ticket['ticket_quantity']; ?>
                                        </td>
                                    </tr>
                                </table>
                                
                                <h2>Cinema & Time Start List</h2>
                                
                                <table class="ui table segment">
                                <thead>
                                  <tr><th>Name</th>
                                  <th>Cinema</th>
                                  <th>Time Start</th>
                                  <th>Available Seats</th>
                                  <th>Price (USD)</th>
<!--                                  <th>Total Seats</th>-->
                                  <th>Accept Actions</th>
                                </tr></thead>
                                <tbody>
                                    <?php 
                                    $list = api_tickets::get_all_tickets_by_ticket_booking(
                                            $ticket['ticket_quantity'], $ticket['city_id'], $ticket['movie_id'], $ticket['show_date']);
                                    if (empty($list) || count($list) == 0) {
                                        echo "<tr><td>No item found.</td></tr>";
                                    } else {
                                        $current_page = 1;
                                        if (!empty($_GET['p'])) {
                                                $current_page = $_GET['p'];
                                        }

                                        $list_total = $list;
                                        $page_size = 10;
                                        $lib_pager = new lib_pager($list_total, $page_size);
                                        $total_page = $lib_pager->get_total_page();
                                        $current_page_list = $lib_pager->get_current_page_list($current_page);
                                        foreach ($current_page_list as $item) {?>

                                    <tr>
                                    <td><?php echo $item['name']; ?></td>
                                    <td><strong><?php echo $item['cinema_name']; ?></strong></td>
                                    <td>
                                        <strong>
                                        <?php 
                                        $date = date_create($item["time_start"]);
                                        echo date_format($date, "H:i");
                                        ?>
                                        </strong>
                                    </td>
                                    <td><strong><?php echo $item['available_seats']; ?></strong></td>
                                    <td><?php echo $item['price']; ?></td>
                                    <!--<td><?php echo $item['total_seats']; ?></td>-->
                                    <td>
                                        <div onclick="accept(<?php echo $item['id']; ?>);" class="ui icon button">
                                            <img src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS; ?>accept.png"></img>
                                        </div>
                                    </td>
                                    </tr>
                                    <?php
                                        }
                                    }?>
                                </tbody>
                                <tfoot>
                                  <tr><th colspan="10">
                                    <label>Your Choice: </label>
                                    <div id="your_choice" class="ui input">
                                        <?php
                                        $text = "No chosen.";
                                        if (!empty($list) && count($list)) {
                                            $_SESSION['book_info']['ticket_id'] = $list[0]['id'];
                                            $date = date_create($list[0]['time_start']);
                                            $text = $list[0]['cinema_name'] . " - " . date_format($date, "H:i");
                                        }
                                        echo $text;
                                        ?>
                                    </div>
                                    <table class="paging-footer">
                                            <tbody>
                                                    <tr>
                                                            <?php 
                                                            if (!empty($total_page) && count($total_page) > 0) {
                                                                echo "<td>Page </td>";
                                                                for ($page = 1; $page <= $total_page; $page++) { ?>
                                                                <td>
                                                                        <span>
                                                                                <a href="?p=<?php echo $page; ?>" <?php if ($current_page != $page) echo 'style="color:Black;"'; ?>><?php echo $page; ?></a>
                                                                        </span>
                                                                </td>
                                                            <?php }
                                                            }?>
                                                    </tr>
                                            </tbody>
                                    </table>
                                  </th>
                                </tr></tfoot>
                              </table>
                                
                                <div class="ui orange segment">
                                    <div class="ts-center">
                                        <div class="ui buttons">
                                            <!--<div class="ui green button">Back</div>-->
                                            <a href="index.php" class="ui purple button">Exit</a>
                                            <div class="or"></div>
                                            <!--<div class="ui orange button">Next</div>-->
                                            <a href="seat.php" class="ui orange button">Next</a>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
<!-- ACCEPT MODAL -->
<div id="accepted" class="ui modal">
  <i class="close icon"></i>
  <div class="header">
    Accepted
  </div>
  <div id="accepted_content" class="content">

  </div>
  <div class="actions">
    <div class="ui orange button">
      OK
    </div>
  </div>
</div>
<!-- END ACCEPT MODAL -->
                <div class="row">
                    <?php
                    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
                    ?>
                </div>
            </div>
        </div>
    </body>
</html>